package cn.forlce;


/**
 * 快速排序算法
 */
public class Demo4 {


    private int[] originSort;

    public void quickSort(int left,int right){
        int temp,i,j,t;
        if (left>right){
//            printer(originSort);
            return;
        }
        temp = originSort[left];
        i = left;
        j = right;

        while (i != j){
            while (originSort[j]>= temp && i<j){
                j--;
            }
            while (originSort[i]<= temp && i<j){
                i++;
            }
            if (j>i){
                originSort[i]=originSort[i]^originSort[j];
                originSort[j]=originSort[i]^originSort[j];
                originSort[i]=originSort[i]^originSort[j];
            }


        }
        //最终将基准数归位
        originSort[left]=originSort[i];
        originSort[i] = temp;
        //递归过程
        quickSort(left,i-1);
        quickSort(i+1,right);

    }

    public static void main(String[] args) {
        int[] a = {6,5,8,4,10,6,9,6,5,5,4,5,3,1,0,2,};
        printer(a);
        Demo4 demo4 = new Demo4();
        demo4.originSort=a;
        demo4.quickSort(0,a.length-1);
        System.out.println("排序后");
        printer(demo4.originSort);
    }
    public static void printer (int[] a){
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+"\t");
        }
        System.out.println();
    }



}
